<template>
  <div class="sidebar">
    <el-menu
        class="sidebar-el-menu"
        :default-active="onRoutes"
        :collapse="sidebar.collapse"
        background-color="#324157"
        text-color="#bfcbd9"
        active-text-color="#20a0ff"
        unique-opened
        router
    >
      <template v-for="item in items">
        <template v-if="item.subs">
          <el-sub-menu :index="item.index" :key="item.index" v-permiss="item.permiss">
            <template #title>
              <el-icon>
                <component :is="item.icon"></component>
              </el-icon>
              <span>{{ item.title }}</span>
            </template>
            <template v-for="subItem in item.subs">
              <el-sub-menu
                  v-if="subItem.subs"
                  :index="subItem.index"
                  :key="subItem.index"
                  v-permiss="subItem.permiss"
              >
                <template #title>{{ subItem.title }}</template>
                <el-menu-item v-for="(threeItem, i) in subItem.subs" :key="i" :index="threeItem.index" v-permiss="threeItem.permiss">
                  {{ threeItem.title }}
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item v-else :index="subItem.index" v-permiss="subItem.permiss">
                {{ subItem.title }}
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else>
          <el-menu-item :index="item.index" :key="item.index" v-permiss="item.permiss">
            <el-icon>
              <component :is="item.icon"></component>
            </el-icon>
            <template #title>{{ item.title }}</template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import {computed, reactive} from 'vue';
import {useSidebarStore} from '../store/sidebar';
import {useRoute} from 'vue-router';
import {useMenuStore} from "@/store/menu";

const menu = useMenuStore();

interface MenuTreeNode {
  icon?: string;
  index: string;
  title: string;
  permiss?: string[];
  subs?: MenuTreeNode[];
}

const items: MenuTreeNode[] = [
  {
    icon: 'Odometer',
    index: '/home',
    title: '首页',
  },
  {
    icon: 'OfficeBuilding',
    index: '/projectMgt',
    title: '项目（小区）管理',
    permiss: ['superAdmin'],
    subs: [
      {
        index: '/projectMgt/index',
        title: '小区管理',
        permiss: ['superAdmin'],
      },
    ]
  },
  {
    icon: 'User',
    index: '/userMgt',
    title: '用户管理',
    permiss: ['admin', 'superAdmin'],
    subs: [
      {
        index: '/userMgt/index',
        title: '用户管理',
        permiss: ['admin', 'superAdmin'],
      },
    ]
  },
  {
    icon: 'Files',
    index: '/parkingMgt',
    title: '车场管理',
    subs: [
      {
        index: '/parkingMgt/index',
        title: '车场管理',
        permiss: ['admin', 'superAdmin'],
      },
      {
        index: '/parkingMgt/lane',
        title: '车道管理',
      },
      {
        index: '/parkingMgt/car',
        title: '车辆管理',
      }
    ]
  },
];

// const items = computed(() => {
//   return [
//     {
//       icon: 'Odometer',
//       index: '/home',
//       title: '首页',
//       permiss: '1',
//     },
//     ...menu.menuList
//   ]
// });

const route = useRoute();
const onRoutes = computed(() => {
  return route.path;
});

const sidebar = useSidebarStore();
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
  z-index: 1;
}

.sidebar::-webkit-scrollbar {
  width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}

.sidebar > ul {
  height: 100%;
}
</style>
